import { TABKEY } from '../config/index'
import {CHANGE_TAB, GET_FILTER_DATA, CHANGE_FILTER, CHANGE_IS_SHOW, GET_FILTER_RESULT} from '../actions/actionTypes'
const tabs = {};

tabs[TABKEY.cate] = {
    key: TABKEY.cate,
    text: '全部分类',
    obj: {}
}

tabs[TABKEY.type] = {
    key: TABKEY.type,
    text: '综合排序',
    obj: {}
}

tabs[TABKEY.filter] = {
    key: TABKEY.filter,
    text: '筛选',
    obj: {}
}

// 初始化state
const initState = {
    tabs: tabs,
    activeKey: '',
    filterData: {},
    openPanelKey: TABKEY.cate, // 打开显示key项的内容
    isShowPanel: false // 默认不展示面板
}

// 更改tab操作
const changeTabOper = (state, action) => {
    return { ...state, activeKey: action.obj.activeKey }
}

// 更改面板展示状态
const changeisshow = (state, action) => {
    return { ...state, isShowPanel: action.obj.isShowPanel }
}

// 获取筛选数据
const getFilterData = (state, action) => {
    return { ...state, filterData: action.obj.data}
}

// 更改要打开的类型
const setOpenKey = (state, action) => {
    return { ...state, openPanelKey: action.obj.openPanelKey }
}

// 获取筛选结果
const getFilterResult = (state, action) => {
    let _tabs = JSON.parse(JSON.stringify(state.tabs));
    _tabs[action.obj.key] = {
        key: action.obj.key,
        text: action.obj.item.name,
        obj: action.obj.item
    };
    return {...state, tabs: _tabs};
}


const handleTabReducer = (state = initState, action) => {
    switch (action.type) {
        case CHANGE_TAB:
            return changeTabOper(state, action);
        case GET_FILTER_DATA:
            return getFilterData(state, action);
        case CHANGE_FILTER:
            return setOpenKey(state, action);
        case CHANGE_IS_SHOW:
            return changeisshow(state, action);
        case GET_FILTER_RESULT:
            return getFilterResult(state, action);
        default:
            return state;
    }
}


export default handleTabReducer;
